import java.util.Objects;

/**
 * @description:
 * @Author: wuzhenbin
 * @CreateTime: 2025-02-14
 */
public class leetcode_206 {
    public static void main(String[] args) {
        ListNode listNode = new ListNode(1, new ListNode(2, null));
        System.out.println(reverseList(listNode));
    }

    public static ListNode reverseList(ListNode head) {
        if(head==null)return null;
        ListNode headNode = new ListNode(0, head);
        while (head.next != null) {
            ListNode tmp = head.next;
            head.next = tmp.next;
            tmp.next = headNode.next;
            headNode.next = tmp;
        }
        return headNode.next;
    }

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        @Override
        public String toString() {
            return "ListNode{" +
                    "val=" + val +
                    ", next=" + next +
                    '}';
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            ListNode listNode = (ListNode) o;
            return val == listNode.val && Objects.equals(next, listNode.next);
        }

        @Override
        public int hashCode() {
            return Objects.hash(val, next);
        }
    }
}
